package com.cls.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cls.business.entity.Commitment;
import com.cls.business.service.ICommitmentService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.utils.MyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class CommitmentController extends BaseController {

    private final ICommitmentService commitmentService;


    /**
     * 详情页面 biz/commitment
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/commitment/{commitmentId}")
    public String commitmentDetail(@PathVariable Long commitmentId, Model model) {
        Commitment commitment = commitmentService.getById(commitmentId);
        model.addAttribute("commitment", commitment);
        return MyUtil.view("system/commitment/commitmentDetail");
    }

    /**
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/commitment")
    public String commitmentIndex() {
        return MyUtil.view("system/commitment/commitment");
    }

    /**
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/commitment/add")
    public String commitmentAdd() {
        return MyUtil.view("system/commitment/addCommitment");
    }

    /**
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/commitment/detail/{id}")
    public String commitmentDetail(Model model, @PathVariable Long id) {
        Commitment commitment = this.commitmentService.getById(id);
        model.addAttribute("commitment", commitment.getContext());
        return MyUtil.view("system/commitment/commitmentDetail");
    }
    /**
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/commitment/update/{id}")
    public String commitmentUpdate(Model model, @PathVariable Long id) {
        Commitment commitment = this.commitmentService.getById(id);
        model.addAttribute("commitment", commitment);
        return MyUtil.view("system/commitment/updateCommitment");
    }


    @GetMapping("biz/commitment/list")
    @ResponseBody
    public ResponseVO getAllMemberInfos(QueryRequest request, Commitment commitment) {

        LambdaQueryWrapper<Commitment> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(commitment) && Objects.nonNull(commitment.getContext())) {
            wrapper.like(Commitment::getContext, commitment.getContext());
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Commitment> list = commitmentService.list(wrapper);
        PageInfo pageInfo = new PageInfo(list);
        return ResponseVO.success(pageInfo);
    }

    @PostMapping("biz/commitment/add")
//    @RequiresPermissions("commitment:add")
    @ResponseBody
    @ControllerEndpoint(operation = "新增承诺书", exceptionMessage = "新增承诺书失败")
    public ResponseVO addCommitment(Commitment commitment) {
        this.commitmentService.save(commitment);
        return ResponseVO.success();
    }

    @PostMapping("biz/commitment/delete/{commitmentId}")
    @ResponseBody
    /* @RequiresPermissions("commitment:delete")*/
    @ControllerEndpoint(operation = "删除承诺书", exceptionMessage = "删除承诺书失败")
    public ResponseVO deleteCommitments( @PathVariable Long commitmentId) {
        this.commitmentService.removeById(commitmentId);
        return ResponseVO.success();
    }

    @PostMapping("biz/commitment/update")
    @ResponseBody
    @ControllerEndpoint(operation = "修改承诺书", exceptionMessage = "修改承诺书失败")
    public ResponseVO updateCommitment(Commitment commitment) {
        this.commitmentService.updateById(commitment);
        return ResponseVO.success();
    }


    @PostMapping("biz/commitment/enable")
    @ResponseBody
    @ControllerEndpoint(operation = "修改承诺书", exceptionMessage = "修改承诺书失败")
    public ResponseVO enableCommitment(Commitment commitment) {
        this.commitmentService.updateEnable(commitment);
        return ResponseVO.success();
    }
}
